package com.moon.star.vtalk;

import com.vtalk.common.servant.entity.ICommon;
import com.vtalk.common.util.ProtoUtil;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author liupei
 * @version 1.0
 * @describe TODO
 * @date 2022/6/9 9:59
 **/
public abstract class VtalkTestUtil {
    public static ICommon.Rsp execute(String url,String token,byte[] body) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("token",token);
        connection.setDoOutput(true);
        connection.getOutputStream().write(body);
        connection.getOutputStream().flush();
        connection.connect();
        if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
            return ProtoUtil.getMessage(connection.getInputStream(), ICommon.Rsp.newBuilder());
        }
        throw new RuntimeException("接口调用失败："+connection.getResponseCode());
    }
}
